<?php
if(isset($message) == TRUE) :
?>
<div class="notification <?php echo $message['type'] ?> png_bg">
    <a class="close" href="#"><img alt="close" title="Đóng" src="<?php echo Url::site('assets/admin/images/icons/cross_grey_small.png') ?>"></a>
    <div><?php echo $message['content'] ?></div>
</div>
<div class="clear"></div>
<?php
endif;
?>

    <div class="content-box"><!-- Start Content Box -->
        <div class="content-box-header">
        <h3>Banner Quảng Cáo</h3>
        <div class="clear"></div>
    </div> <!-- End .content-box-top -->

        <div class="content-box-content">

        <div id="tab1" class="tab-content default-tab" style="display: block;">
            <h3>Thay đổi vị trí hiển thị banner ở trang chủ</h3>
            <p>Kéo thả các banner vào vị trí muốn hiển thị</p>
            <p>Click vào banner để edit liên kết</p>
            <div id="layout">
                <div class="top drop_me">
<?php
if(isset ($top) == TRUE && count($top) > 0) :
    foreach($top as $obj) :
?>
                <img class="droppable_item" rel="<?php echo $obj->url; ?>" src="<?php echo Url::site('assets/upload/banner/' . $obj->image) ?>" alt="" id="<?php echo $obj->id ?>" width="100" />
<?php
    endforeach;
endif;
?>
                </div>

                <div class="left drop_me">
<?php
if(isset ($left) == TRUE && count($left) > 0) :
    foreach($left as $obj) :
?>
                <img class="droppable_item" rel="<?php echo $obj->url; ?>" src="<?php echo Url::site('assets/upload/banner/' . $obj->image) ?>" alt="" id="<?php echo $obj->id ?>" width="100" />
<?php
    endforeach;
endif;
?>
                </div>
                <div class="right drop_me">
<?php
if(isset ($right) == TRUE && count($right) > 0) :
    foreach($right as $obj) :
?>
                <img class="droppable_item" rel="<?php echo $obj->url; ?>" src="<?php echo Url::site('assets/upload/banner/' . $obj->image) ?>" alt="" id="<?php echo $obj->id ?>" width="100" />
<?php
    endforeach;
endif;
?>
                </div>
                <div class="clear"></div>

                <div class="bottom drop_me">
<?php
if(isset ($bottom) == TRUE && count($bottom) > 0) :
    foreach($bottom as $obj) :
?>
                <img class="droppable_item" rel="<?php echo $obj->url; ?>" src="<?php echo Url::site('assets/upload/banner/' . $obj->image) ?>" alt="" id="<?php echo $obj->id ?>" width="100" />
<?php
    endforeach;
endif;
?>
                </div>
            </div>
            <h3>Đối Tác</h3>
            <div class="items">
<?php
if(isset ($partner) == TRUE && count($partner) > 0) :
    foreach($partner as $obj) :
?>
                <img class="droppable_item" rel="<?php echo $obj->url; ?>" src="<?php echo Url::site('assets/upload/banner/' . $obj->image) ?>" alt="" id="<?php echo $obj->id ?>" width="100" />
<?php
    endforeach;
endif;
?>
            </div>
            <h3>Xóa banner</h3>
            <div id="trash">Kéo thả banner hoặc đối tác vào đây để xóa</div>
        </div> <!-- End #tab1 -->
    </div> <!-- End .content-box-content -->

</div>

<script type="text/javascript">
$(function(){
    Sidebar.setActive(Sidebar.menu.ads, 1);

    $('img.droppable_item').draggable({
        appendTo: 'body',
        helper: 'clone'
    });

    $('#layout').children('div').droppable({
        hoverClass: "ui-state-hover",
        drop: function( event, ui ) {
            _this = $(this);
            _position = _this.attr('class');
            _position = _position.substring(0, _position.indexOf(' '));
            
            ui.draggable.appendTo(this);

            url = '<?php echo Url::site('admin/ads/update') ?>';
            data = {
                position: _position,
                id      : ui.draggable.attr('id')
            };
            $.post(url, data);
        }
    });

    $('#trash').droppable({
        drop: function(event, ui) {
            var choice = confirm('Bạn có chắc muốn xóa?');
            if(choice == true) {
                ui.draggable.appendTo(this).fadeOut(function() {
                    $(this).remove();
                });;

                url = '<?php echo Url::site('admin/ads/delete') ?>';
                data = {
                    id : ui.draggable.attr('id')
                };
                $.post(url, data);
            }
        }
    });
    
    //---------------------- Edit link for ads -----------------------------//
    //---------------------- Edit by thanh.dang ----------------------------//    
    var _ad_el = {};

    var _menu_container = $('<div class="menu-cmd"></div>');

    var _menu_command = $(  '<a class="edit" href="javascript:void(0)"><span>Link</span></a>'+
                            '<a class="delete" href="javascript:void(0)"><span>Xoá</span></a>'+
                            '<a class="close" href="javascript:void(0)"><span>&nbsp;</span></a>'
                        );

    var _input_field =  $(
                            '<label>Liên kết: </label>'+
                            '<input type="text" name="link" />'+
                            '<a class="button" href="javascript:void(0)">Cập nhật</a>'+
                            '<a class="close" href="javascript:void(0)"><span>&nbsp;</span></a>'
                        );

    $('img.droppable_item').bind('click',function(){

        // get ad's id
        _ad_el = $(this);

        // display menu bar
        var _offset = $(this).offset();
        $(this).after(_menu_container.css({
            position: 'absolute',
            top  : _offset.top + $(this).height(),
            left : _offset.left
        }).html(_menu_command));            
    });

    //--------------------------- COMAND ----------------------------//

    // close menu bar event
    $('a.close').live('click',function(){ _menu_container.remove(); });
    
    // edit ad's link action
    $('a.edit').live('click',function(){
        _menu_container.html(_input_field);
        $('input[name=link]').val(_ad_el.attr('rel'))
    });

    // update ad's link action    
    $('a.button').live('click',function(){
        var _url = '<?php echo Url::site('admin/ads/update_link') ?>';

        var _data = {
            id   : _ad_el.attr('id'),
            link : $('input[name=link]').val()
        };

        $.post(_url,_data).done(function(){
            _ad_el.attr('rel',_data.link)
        });
        _menu_container.remove();
    });

    // delete ad action
    $('a.delete').live('click',function(){
        var _url = '<?php echo Url::site('admin/ads/delete') ?>';
        data = {
            id : _ad_el.attr('id')
        };
        
        if(confirm('Bạn có chắc muốn xóa banner này ?')){
            $.post(_url, data).done(function(){
                _ad_el.remove();
                _menu_container.remove();
            });
        }
    });
});
</script>